home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / gnused.zip / GNUSED / UTILS.C < prev   
C/C++ Source or Header  |  1992-11-06  |  6KB  |  361 lines

  1. /*  Functions from hack's utils library.
  2.     Copyright (C) 1989-1991 Free Software Foundation, Inc.
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* These routines were written as part of a library (by hack), but since most
  19.    people don't have the library, here they are.  */
  20.  
  21. #ifdef __STDC__
  22. #define VOID void
  23. #else
  24. #define VOID char
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #if defined(USG) || defined(STDC_HEADERS)
  29. #include <string.h>
  30. #define bcopy(src, dst, len) memcpy((dst), (src), (len))
  31. #else
  32. #include <strings.h>
  33. #endif
  34. #if defined(STDC_HEADERS)
  35. #include <stdlib.h>
  36. #else
  37. VOID *malloc();
  38. VOID *realloc();
  39. #endif
  40.  
  41. VOID *ck_malloc();
  42.  
  43. char *myname;
  44.  
  45. #ifdef __STDC__
  46. #include <stdarg.h>
  47.  
  48. /* Print an error message and exit */
  49. void
  50. panic(char *str, ...)
  51. {
  52.     va_list iggy;
  53.  
  54.     fprintf(stderr,"%s: ",myname);
  55.     va_start(iggy,str);
  56. #ifdef NO_VFPRINTF
  57.     _doprnt(str,&iggy,stderr);
  58. #else
  59.     vfprintf(stderr,str,iggy);
  60. #endif
  61.     va_end(iggy);
  62.     putc('\n',stderr);
  63.     exit(4);
  64. }
  65.  
  66. #else
  67. #include <varargs.h>
  68.  
  69. void
  70. panic(str,va_alist)
  71. char *str;
  72. va_dcl
  73. {
  74.     va_list iggy;
  75.  
  76.     fprintf(stderr,"%s: ",myname);
  77.     va_start(iggy);
  78. #ifdef NO_VFPRINTF
  79.     _doprnt(str,&iggy,stderr);
  80. #else
  81.     vfprintf(stderr,str,iggy);
  82. #endif
  83.     va_end(iggy);
  84.     putc('\n',stderr);
  85.     exit(4);
  86. }
  87.  
  88. #endif
  89.  
  90. /* Store information about files opened with ck_fopen
  91.    so that error messages from ck_fread, etc can print the
  92.    name of the file that had the error */
  93. #define N_FILE 32
  94.  
  95. struct id {
  96.     FILE *fp;
  97.     char *name;
  98. };
  99.  
  100. static struct id __id_s[N_FILE];
  101.  
  102. /* Internal routine to get a filename from __id_s */
  103. char *
  104. __fp_name(fp)
  105. FILE *fp;
  106. {
  107.     int n;
  108.  
  109.     for(n=0;n<N_FILE;n++) {
  110.         if(__id_s[n].fp==fp)
  111.             return __id_s[n].name;
  112.     }
  113.     return "{Unknown file pointer}";
  114. }
  115.  
  116. /* Panic on failing fopen */
  117. FILE *
  118. ck_fopen(name,mode)
  119. char *name;
  120. char *mode;
  121. {
  122.     FILE    *ret;
  123.     int    n;
  124.  
  125.     ret=fopen(name,mode);
  126.     if(ret==(FILE *)0)
  127.         panic("Couldn't open file %s",name);
  128.     for(n=0;n<N_FILE;n++) {
  129.         if(ret==__id_s[n].fp) {
  130.             free((VOID *)__id_s[n].name);
  131.             __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  132.             strcpy(__id_s[n].name,name);
  133.             break;
  134.         }
  135.     }
  136.     if(n==N_FILE) {
  137.         for(n=0;n<N_FILE;n++)
  138.             if(__id_s[n].fp==(FILE *)0)
  139.                 break;
  140.         if(n==N_FILE)
  141.             panic("Internal error: too many files open");
  142.         __id_s[n].fp=ret;
  143.         __id_s[n].name=(char *)ck_malloc(strlen(name)+1);
  144.         strcpy(__id_s[n].name,name);
  145.     }
  146.     return ret;
  147. }
  148.  
  149. /* Panic on failing fwrite */
  150. void
  151. ck_fwrite(ptr,size,nmemb,stream)
  152. char *ptr;
  153. int size,nmemb;
  154. FILE *stream;
  155. {
  156.     if(fwrite(ptr,size,nmemb,stream)!=nmemb)
  157.         panic("couldn't write %d items to %s",nmemb,__fp_name(stream));
  158. }
  159.  
  160. /* Panic on failing fclose */
  161. void
  162. ck_fclose(stream)
  163. FILE *stream;
  164. {
  165.     if(fclose(stream)==EOF)
  166.         panic("Couldn't close %s",__fp_name(stream));
  167. }
  168.  
  169. /* Panic on failing malloc */
  170. VOID *
  171. ck_malloc(size)
  172. int size;
  173. {
  174.     VOID *ret;
  175.  
  176.     if(!size)
  177.         size++;
  178.     ret=malloc(size);
  179.     if(ret==(VOID *)0)
  180.         panic("Couldn't allocate memory");
  181.     return ret;
  182. }
  183.  
  184. /* Panic on failing realloc */
  185. VOID *
  186. ck_realloc(ptr,size)
  187. VOID *ptr;
  188. int size;
  189. {
  190.     VOID *ret;
  191.  
  192.     ret=realloc(ptr,size);
  193.     if(ret==(VOID *)0)
  194.         panic("Couldn't re-allocate memory");
  195.     return ret;
  196. }
  197.  
  198. /* Return a malloc()'d copy of a string */
  199. char *
  200. ck_strdup(str)
  201. char *str;
  202. {
  203.     char *ret;
  204.  
  205.     ret=(char *)ck_malloc(strlen(str)+2);
  206.     strcpy(ret,str);
  207.     return ret;
  208. }
  209.  
  210. #if !defined(USG) && !defined(STDC_HEADERS)
  211. /*
  212.  * memchr - search for a byte
  213.  *
  214.  */
  215.  
  216. VOID *
  217. memchr(s, ucharwanted, size)
  218. VOID *s;
  219. int ucharwanted;
  220. int size;
  221. {
  222.     register char *scan;
  223.     register n;
  224.     register uc;
  225.  
  226.     scan = (char *)s;
  227.     uc = (ucharwanted&0xFF);
  228.     for (n = size; n > 0; n--)
  229.         if (((*scan)&0xFF) == uc)
  230.             return((VOID *)scan);
  231.         else
  232.             scan++;
  233.  
  234.     return 0;
  235. }
  236. #endif
  237.  
  238. #if !defined(STDC_HEADERS)
  239. /*
  240.  * memmove - copy bytes, being careful about overlap.
  241.  */
  242.  
  243. VOID *
  244. memmove(dst, src, size)
  245. VOID *dst;
  246. VOID *src;
  247. int size;
  248. {
  249.     register char *d;
  250.     register char *s;
  251.     register int n;
  252.  
  253.     if (size <= 0)
  254.         return(dst);
  255.  
  256.     s = (char *)src;
  257.     d = (char *)dst;
  258.     if (s <= d && s + (size-1) >= d) {
  259.         /* Overlap, must copy right-to-left. */
  260.         s += size-1;
  261.         d += size-1;
  262.         for (n = size; n > 0; n--)
  263.             *d-- = *s--;
  264.     } else
  265.         for (n = size; n > 0; n--)
  266.             *d++ = *s++;
  267.  
  268.     return(dst);
  269. }
  270. #endif
  271.  
  272. /* Implement a variable sized buffer of 'stuff'.  We don't know what it is,
  273.    nor do we care, as long as it doesn't mind being aligned by malloc. */
  274.  
  275. struct buffer {
  276.     int    allocated;
  277.     int    length;
  278.     char    *b;
  279. };
  280.  
  281. #define MIN_ALLOCATE 50
  282.  
  283. VOID *
  284. init_buffer()
  285. {
  286.     struct buffer *b;
  287.  
  288.     b=(struct buffer *)ck_malloc(sizeof(struct buffer));
  289.     b->allocated=MIN_ALLOCATE;
  290.     b->b=(char *)ck_malloc(MIN_ALLOCATE);
  291.     b->length=0;
  292.     return (VOID *)b;
  293. }
  294.  
  295. void
  296. flush_buffer(bb)
  297. VOID *bb;
  298. {
  299.     struct buffer *b;
  300.  
  301.     b=(struct buffer *)bb;
  302.     free(b->b);
  303.     b->b=0;
  304.     b->allocated=0;
  305.     b->length=0;
  306.     free(b);
  307. }
  308.  
  309. int
  310. size_buffer(b)
  311. VOID *b;
  312. {
  313.     struct buffer *bb;
  314.  
  315.     bb=(struct buffer *)b;
  316.     return bb->length;
  317. }
  318.  
  319. void
  320. add_buffer(bb,p,n)
  321. VOID *bb;
  322. char *p;
  323. int n;
  324. {
  325.     struct buffer *b;
  326.  
  327.     b=(struct buffer *)bb;
  328.     if(b->length+n>b->allocated) {
  329.         b->allocated*=2;
  330.         b->b=(char *)ck_realloc(b->b,b->allocated);
  331.     }
  332.     bcopy(p,b->b+b->length,n);
  333.     b->length+=n;
  334. }
  335.  
  336. void
  337. add1_buffer(bb,ch)
  338. VOID *bb;
  339. int ch;
  340. {
  341.     struct buffer *b;
  342.  
  343.     b=(struct buffer *)bb;
  344.     if(b->length+1>b->allocated) {
  345.         b->allocated*=2;
  346.         b->b=(char *)ck_realloc(b->b,b->allocated);
  347.     }
  348.     b->b[b->length]=ch;
  349.     b->length++;
  350. }
  351.  
  352. char *
  353. get_buffer(bb)
  354. VOID *bb;
  355. {
  356.     struct buffer *b;
  357.  
  358.     b=(struct buffer *)bb;
  359.     return b->b;
  360. }
  361.